import { IParam } from "@/interface";

/**
 * @description 通过统一资源标识计算菜单
 * @export
 * @param {IParam} modelParam
 * @param {IParam} runtimeParam
 */
export function handleMenusResource(modelParam: IParam, runtimeParam: IParam) {
  const { menus, authService, defaultOpens } = runtimeParam;
  computedEffectiveMenus(menus.value, authService);
  handleDefaultOpens(menus.value, defaultOpens.value);
  handleDefaultSelect(modelParam, runtimeParam);
}

/**
 * @description 菜单项选中
 * @param {IParam} modelParam 
 * @param {IParam} runtimeParam
 */
export function menuSelect(modelParam: IParam, runtimeParam: IParam) {
  let { menus, defaultSelect } = runtimeParam;
  let item = compute(menus.value, defaultSelect.value[0]);
  if (Object.keys(item).length > 0) {
    menuClick(item, modelParam, runtimeParam);
  }
}

/**
 * @description 计算有效菜单项
 * @param {IParam[]} menus 菜单集合
 * @param {IParam} authService 权限服务
 */
function computedEffectiveMenus(menus: IParam[], authService: IParam) {
  menus.forEach((item: any) => {
    // if (!authService?.getMenusPermission?.(item)) {
    //   item.hidden = true;
    // }
    if (Object.is(item.id, '3')) {
      item.hidden = true;
    }
    if (item.items?.length > 0) {
      computedEffectiveMenus(item.items, authService);
    }
  })
}

/**
 * @description 处理菜单项默认展开
 * @param {IParam[]} menus 菜单集合
 * @param {string[]} defaultOpens 默认展开数组
 */
function handleDefaultOpens(menus: IParam[], defaultOpens: string[]) {
  menus.forEach((item: any) => {
    if (item.expanded) {
      defaultOpens = [...defaultOpens,item.id];
    }
    if (item.items?.length > 0) {
      handleDefaultOpens(item.items, defaultOpens);
    }
  });
}

/**
 * @description 处理菜单默认选中
 * @param {IParam} modelParam
 * @param {IParam} runtimeParam
 */
function handleDefaultSelect(modelParam: IParam, runtimeParam: IParam) {
  const { funcs } = modelParam;
  const { menus, defaultSelect, defaultView, route, router, mode } = runtimeParam;
  if (route?.matched?.length == 2) { // 存在二级路由
    const [{ }, matched] = route.matched;
    const appFunc: any = funcs.find((func: any) => Object.is(func.routePath, matched.path) && Object.is(func.funcType, 'APPVIEW'));
    if (appFunc) {
      computeMenuSelect(menus.value, appFunc.appFuncTag, funcs, runtimeParam);
    }
    return;
  } else if (defaultView) { // 存在默认视图
    const appFunc: any = funcs.find((func: any) => Object.is(func.appView, defaultView) && Object.is(func.funcType, 'APPVIEW'));
    if (appFunc) {
      computeMenuSelect(menus.value, appFunc.appFuncTag, funcs, runtimeParam);
    } 
    if (!defaultSelect.value[0] && Object.is(mode, "NONE")) {
      // TODO
      return;
    } 
  } else {
    computeMenuSelect(menus.value, '', funcs, runtimeParam);
  }
  let item = compute(menus.value, defaultSelect.value[0]);
  if (Object.keys(item).length > 0 && !item.hidden) {
    menuClick(item, modelParam, runtimeParam);
  }
}

/**
 * @description 计算菜单选中项
 * @param {IParam[]} menus 菜单项集合
 * @param {string} funcTag 应用功能标识
 * @param {IParam} funcs 应用功能集合
 * @param {string[]} runtimeParam 运行时
 * @return {*} 
 */
function computeMenuSelect(menus: IParam[], funcTag: string, funcs: IParam, runtimeParam: IParam) {
  let { defaultSelect, defaultOpens } = runtimeParam;
  return menus.some((item: any) => {
    if (Object.is(funcTag, '') && item.funcTag && item.openDefault && !item.hidden) {
        const appFunc = funcs?.find((func: any) => Object.is(func.funcTag, item.funcTag));
        if (appFunc) {
            defaultSelect.value = [item.id];
            return true;
        }
    }
    if (item.funcTag && Object.is(item.funcTag, funcTag)) {
        defaultSelect.value = [item.id];
        return true;
    }
    if (item.items?.length > 0) {
        const state = computeMenuSelect(item.items, funcTag, funcs, runtimeParam);
        if (state) {
            const defaultOpen = defaultOpens.value?.find((open: string) => Object.is(item.id, open));
            if (!defaultOpen) {
              defaultOpens.value = [...defaultOpens.value,item.id];
            }
            return true;
        }
    }
    return false;
  });
}

/**
 * @description 获取指定菜单项数据
 * @param {IParam[]} menus 菜单集合
 * @param {string} id 菜单标识
 */
function compute(menus: IParam[], id: string) {
  const item: any = {};
  menus.some((_item: any) => {
    if (Object.is(_item.id, id)) {
      Object.assign(item, _item);
      return true;
    }
    if (_item.items?.length > 0) {
      const subItem = compute(_item.items, id);
      if (Object.keys(subItem).length > 0) {
        Object.assign(item, subItem);
        return true;
      }
    }
    return false;
  });
  return item;
}

/**
 * @description 菜单项点击
 * @param {IParam} item 选中菜单项
 * @param {IParam} modelParam 
 * @param {IParam} runtimeParam 
 */
function menuClick(item: IParam, modelParam: IParam, runtimeParam: IParam) {
  const { router } = runtimeParam;
  router.push('/AppIndexView/editview');
}